package com.llap.exp190929.controller;

import com.llap.exp190929.dto.PaginatedDto;
import com.llap.exp190929.entity.District;
import com.llap.exp190929.service.DistrictService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * Created by LLAP on 2019/10/14.
 * Copyright (c) 2019 LLAP. All rights reserved.
 */
@RestController
@RequestMapping("districts")
@AllArgsConstructor
@CrossOrigin
@Slf4j
public class DistrictsController {
    private DistrictService districtService;

    @GetMapping("")
    public PaginatedDto<District> findDistricts(
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "20") Integer pageSize) {
        return PaginatedDto.from(districtService.findDistricts(page, pageSize));
    }

    @GetMapping("{id}")
    public District findDistrict(@PathVariable Integer id) {
        return districtService.findDistrict(id);
    }

    @PostMapping("")
    public District addDistrict(@RequestBody District district) {
        return districtService.addDistrict(district);
    }

    @DeleteMapping("")
    public void deleteDistrict(@RequestParam Integer id) {
        districtService.deleteDistrict(id);
    }

    @PatchMapping("")
    public District updateDistrict(@RequestBody District district) {
        return districtService.updateDistrict(district);
    }
}
